<template>
  <el-dialog
    :title="!dataForm.brandId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="品牌名称" prop="brandName">
      <el-input v-model="dataForm.brandName" placeholder="品牌名称"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="telephone">
      <el-input v-model="dataForm.telephone" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="品牌网络" prop="brandWeb">
      <el-input v-model="dataForm.brandWeb" placeholder="品牌网络"></el-input>
    </el-form-item>
    <el-form-item label="品牌logo" prop="brandLogo">
<!--      <el-input v-model="dataForm.brandLogo" placeholder="品牌logo URL"></el-input>-->
      <single-upload v-model="dataForm.brandLogo"></single-upload>
    </el-form-item>
    <el-form-item label="品牌描述" prop="brandDesc">
      <el-input v-model="dataForm.brandDesc" placeholder="品牌描述"></el-input>
    </el-form-item>
    <el-form-item label="品牌状态,0禁用,1启用" prop="brandStatus">
      <el-input v-model="dataForm.brandStatus" placeholder="品牌状态,0禁用,1启用"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="brandOrder">
      <el-input v-model="dataForm.brandOrder" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="最后修改时间" prop="modifiedTime">
      <el-input v-model="dataForm.modifiedTime" placeholder="最后修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from '@/components/upload/singleUpload'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        brandId: 0,
        brandName: '',
        telephone: '',
        brandWeb: '',
        brandLogo: '',
        brandDesc: '',
        brandStatus: '',
        brandOrder: '',
        modifiedTime: '',
        deleted: ''
      },
      dataRule: {
        brandName: [
            { required: true, message: '品牌名称不能为空', trigger: 'blur' }
        ],
        telephone: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
        ],
        brandWeb: [
            { required: true, message: '品牌网络不能为空', trigger: 'blur' }
        ],
        brandLogo: [
            { required: true, message: '品牌logo URL不能为空', trigger: 'blur' }
        ],
        brandDesc: [
            { required: true, message: '品牌描述不能为空', trigger: 'blur' }
        ],
        brandStatus: [
            { required: true, message: '品牌状态,0禁用,1启用不能为空', trigger: 'blur' }
        ],
        brandOrder: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
        ],
        modifiedTime: [
            { required: true, message: '最后修改时间不能为空', trigger: 'blur' }
        ],
        deleted: [
            { required: true, message: '逻辑删除（0.删除 1.未删除）不能为空', trigger: 'blur' }
        ]
      },
      fileList: []
    }
  },
  components: {
    SingleUpload
  },
  methods: {
    init (id) {
      this.dataForm.brandId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.brandId) {
          this.$http({
            url: this.$http.adornUrl(`/childrenwear_product/product/brandinfo/info/${this.dataForm.brandId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.brandName = data.brandInfo.brandName
              this.dataForm.telephone = data.brandInfo.telephone
              this.dataForm.brandWeb = data.brandInfo.brandWeb
              this.dataForm.brandLogo = data.brandInfo.brandLogo
              this.dataForm.brandDesc = data.brandInfo.brandDesc
              this.dataForm.brandStatus = data.brandInfo.brandStatus
              this.dataForm.brandOrder = data.brandInfo.brandOrder
              this.dataForm.modifiedTime = data.brandInfo.modifiedTime
              this.dataForm.deleted = data.brandInfo.deleted
            }
          })
        }
      })
    },
      // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          console.log(this.dataForm.brandLogo)
          this.$http({
            url: this.$http.adornUrl(`/childrenwear_product/product/brandinfo/${!this.dataForm.brandId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'brandId': this.dataForm.brandId || undefined,
              'brandName': this.dataForm.brandName,
              'telephone': this.dataForm.telephone,
              'brandWeb': this.dataForm.brandWeb,
              'brandLogo': this.dataForm.brandLogo,
              'brandDesc': this.dataForm.brandDesc,
              'brandStatus': this.dataForm.brandStatus,
              'brandOrder': this.dataForm.brandOrder,
              'modifiedTime': this.dataForm.modifiedTime,
              'deleted': this.dataForm.deleted
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
